package com.mine.springmvc.syn;

public class UnsafeBuyTicket {

    public static void main(String[] args) {
        BuyTicket station = new BuyTicket();

        new Thread(station,"小红").start();
        new Thread(station,"小名").start();
        new Thread(station,"黄牛").start();


    }
}


class BuyTicket implements Runnable{


    private int ticketNums = 10;

    private boolean flag = true;

    @Override
    public void run() {
        //买票
        while (flag){
            try {
                buy();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    //synchronized 同步方法，锁的是this
    private synchronized   void buy() throws InterruptedException {
        //
        if(ticketNums <=0){
            return;
        }
        Thread.sleep(100);
        System.out.println(Thread.currentThread().getName()+"拿到"+ticketNums--);

    }


}
